// 客户端网络处理
package com.xie.smfs.network;

import com.xie.smfs.common.events.PlayerEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;

public class ClientSpiritNetworkHandler {
    public static void register() {
        ClientPlayNetworking.registerGlobalReceiver(
            SpiritNetworkHandler.SYNC_SPIRIT_DATA,
            ClientSpiritNetworkHandler::handleSyncSpiritData
        );
    }
    
    private static void handleSyncSpiritData(
        MinecraftClient client, 
        ClientPlayNetworkHandler handler, 
        PacketByteBuf buf, 
        PacketSender sender
    ) {
        NbtCompound spiritData = buf.readNbt();
        client.execute(() -> {
            if (client.player != null) {
                PlayerEvents.setSpiritAttributes(client.player, spiritData);
            }
        });
    }
}